﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CYZH.Common
{
    /// <summary>
    /// 文件路径生成类
    /// </summary>
    public class FileOper
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sourccetype">输入数据类型</param>
        /// <param name="destype">输出数据类型</param>
        /// <param name="qhcode">行政区划代码</param>
        /// <param name="filetime">时间</param>
        /// <returns></returns>
        public static string GenRasterMapFilePath(string filename)
        {
            //string file = Constants.BASE_PATH + sourccetype + "\\result\\"+ destype + "\\" + filename + ".map";
            string file = ConfigHelper.BASE_PATH()+ "mapfiles\\" + filename + ".map";

            if (File.Exists(file))
            {
                File.Delete(file);
            }
            string dir = Path.GetDirectoryName(file);
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
            return file;
        }
        
       
        /// <summary>
        /// 生成SPI栅格文件路径
        /// </summary>
        /// <param name="qhcode"></param>
        /// <param name="filetime"></param>
        /// <param name="iswrite">生成路径 true, 读取路径 false</param>
        /// <returns></returns>
        public static string GenSPIRasterFilePath(string layername)
        {
            string file = ConfigHelper.BASE_PATH()+ "mapfiles\\SPI\\" + layername +  ".tif";
            if (File.Exists(file))
            {
                File.Delete(file);
            }
            string dir = Path.GetDirectoryName(file);
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
          
            return file;
        }
        public static string GenAOIFilePath(string filename)
        {
            string file = ConfigHelper.BASE_PATH() + "AOIFile\\" + filename;


            if (!File.Exists(file))
            {
                file = "";
            }
           
            return file;
        }
        
        public static string GenSPIASCFilePath(string layername)
        {
            string file=ConfigHelper.BASE_PATH() + "mapfiles\\SPI\\" + layername + ".asc";
            if (File.Exists(file))
            {
                File.Delete(file);
            }
            string dir = Path.GetDirectoryName(file);
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
            return file;
        }
        /// <summary>
        /// 根据传入的文件名，生成输入气象文件所在的路径
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="year"></param>
        /// <returns></returns>
        public static string GenASCInFilePath(string filename)
        {
           // string[] rqs = filename.Split('-');
           // string year = rqs[1].Substring(0,4);
            return ConfigHelper.BASE_PATH() + "ASC\\source\\" + filename + ".asc";
        }
        public static string GenModisFilePath(string layerName,string alg)
        {
            string file = ConfigHelper.BASE_PATH() + "mapfiles\\"+alg+"\\" + layerName + ".tif";
            if (File.Exists(file))
            {
                File.Delete(file);
            }
            string dir = Path.GetDirectoryName(file);
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
            return file;
        }
        /// <summary>
        /// NDVI文件路径
        /// </summary>
        /// <param name="basePath"></param>
        /// <param name="year"></param>
        /// <param name="day"></param>
        /// <returns></returns>

        public static string GenModisSourceFilePath(string mtype,string filename, string filetime)
        {
            
            return ConfigHelper.BASE_PATH() + mtype+"\\source\\" + filetime.Substring(0, 4) + "\\" + filename;
        }
        
        

        /// <summary>
        /// 生成已经存在的modis文件全路径
        /// </summary>
        /// <param name="modisType">modis类型  09Q1 11A1</param>
        /// <param name="filename">文件名</param>
        /// <param name="alg">算法</param>
        /// <param name="ext">后缀名</param>
        /// <returns></returns>
        public static string GetExitsModisFilePath(string filename,string alg)
        {
            string file = String.Format("{0}mapfiles\\{1}\\{2}.tif", ConfigHelper.BASE_PATH(),alg,filename);// Constants.BASE_PATH + modisType+"\\result\\" +alg+"\\" + filename+"."+ ext;
            
            return file;
        }
        /// <summary>
        /// 生成新的modis文件全路径
        /// </summary>
        /// <param name="modisType"></param>
        /// <param name="filename"></param>
        /// <param name="alg"></param>
        /// <param name="ext"></param>
        /// <returns></returns>
        public static string GenNewModisFilePath(string modisType, string filename, string alg, string ext)
        {
            string file = String.Format("{0}\\result\\{1}\\{2}.{3}", ConfigHelper.BASE_PATH() + modisType, alg, filename, ext);// Constants.BASE_PATH + modisType+"\\result\\" +alg+"\\" + filename+"."+ ext;
            if (File.Exists(file))
            {
                File.Delete(file);
            }
            string dir = Path.GetDirectoryName(file);
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
            return file;
        }
        public static string GenLSTInFilePath(string filename,string type,string filetime)
        {
            return ConfigHelper.BASE_PATH() + type + "\\source\\" + filetime.Substring(0, 4) + "\\" + filename;
        }
        //public static string GenLSTSaveFilePath(string stime,string rowcol)
        //{
        //    string file = Constants.BASE_PATH + "MOD11A1\\result\\Temp\\LST_" +  stime + "." + rowcol + ".tif";
        //    if (File.Exists(file))
        //    {
        //        File.Delete(file);
        //    }
        //    string dir = Path.GetDirectoryName(file);
        //    if (!Directory.Exists(dir))
        //    {
        //        Directory.CreateDirectory(dir);
        //    }
        //    return file;
        //}
        
        //public static string GenLSTClipFile(string xzqhcode, string stime, string rowcol)
        //{
        //    string file = Constants.BASE_PATH + "MOD11A1\\result\\LST\\LST_" + xzqhcode + "_" + stime + "." + rowcol + ".tif";
        //    if (File.Exists(file))
        //    {
        //        File.Delete(file);
        //    }
        //    string dir = Path.GetDirectoryName(file);
        //    if (!Directory.Exists(dir))
        //    {
        //        Directory.CreateDirectory(dir);
        //    }
        //    return file;
        //}
        public static string GenPreNDVI(string xzqhcode,string year)
        {
            return ConfigHelper.BASE_PATH() + "mapfiles\\NDVI\\" + xzqhcode +"_"+ year + "_ndvi.tif";
        }
        public static string GenPreGrass(string xzqhcode, string year)
        {
            return ConfigHelper.BASE_PATH() + "mapfiles\\GRASS\\" + xzqhcode + "_" + year + "_grass.tif";
        }
        public static string GenSHILST(string filename)
        {
            return ConfigHelper.BASE_PATH() + "MOD11A1\\result\\LST\\" +filename+ ".tif";
        }
        public static string GenTVDIFile(string xzqhcode, string stime, string rowcol)
        {
            string file = ConfigHelper.BASE_PATH() + "MOD11A1\\result\\TVDI\\TVDI_" + xzqhcode + "_" + stime + "." + rowcol + ".tif";
            if (File.Exists(file))
            {
                File.Delete(file);
            }
            string dir = Path.GetDirectoryName(file);
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
            return file;
        }
    }
}
